package com.simple.javatraining.testing.testinside.questions;

import com.simple.javatraining.testing.io.InputOutput;
import com.simple.javatraining.testing.testinside.answers.Answer;
import com.simple.javatraining.testing.testinside.answers.ProposedAnswer;

import java.util.LinkedList;
import java.util.List;

public class QuestionWithSingleAnswer extends Question{

public QuestionWithSingleAnswer(String question, List<ProposedAnswer> answers) {
    super(question,answers);
}

public double checkUserAnswerForCorrect(Answer userAnswer) {
    int chosenIndexOfAnswer = InputOutput.convertStringToInt(userAnswer.getTextOfAnswer()) - 1;
    List<ProposedAnswer> variantsOfAnswer = this.getVariantsOfAnswer();
    double correspondenceTruth = 0d;
    if (variantsOfAnswer.get(chosenIndexOfAnswer).isCorrect()) {
        correspondenceTruth = 1d;
    }
    return correspondenceTruth;
}

public void proposeVariantsOfAnswer() {
    String[] listOfVariants = this.formListOfVariants();
    for(String currentVariant:listOfVariants) {
    		InputOutput.outputMessageToConsole(currentVariant);
    }
}

public void printHint() {
    InputOutput.outputMessageToConsole("Выберите ответ, указав его номер:");
}

public String[] formListOfVariants() {
	List<ProposedAnswer> variantsOfAnswer =
                        new LinkedList<ProposedAnswer>(this.getVariantsOfAnswer());
	String[] variantsWithNumber = new String[variantsOfAnswer.size()];
    for (int currentIndex = 0; currentIndex < variantsOfAnswer.size(); currentIndex++) {
    	variantsWithNumber[currentIndex] = (currentIndex + 1) + ". "
                + variantsOfAnswer.get(currentIndex).getTextOfAnswer();
    }
    return variantsWithNumber;
}
}
